package com.usian.wemedia.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.apis.admin.wemedia.WmUserControllerApi;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.service.WmUserService;
import com.usian.wemedia.service.impl.WmUserServiceImpl;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/user")
public class WmUserController implements WmUserControllerApi {
    @Autowired
    private WmUserServiceImpl userService;

    @PostMapping("/save")
    @Override
    public ResponseResult save(@RequestBody WmUser wmUser) {
        if (wmUser == null || wmUser.getName() == null || wmUser.getApUserId() == null) return userService.errorResult;
        userService.save(wmUser);
        return userService.sucResult;


    }

    @GetMapping("/findByName/{name}")
    @Override
    public WmUser findByName(@PathVariable("name") String name) {
        if (name==null)return null;
        List<WmUser> list = userService.list(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
        return list != null && list.size() != 0 ? list.get(0) : null;
    }
    @GetMapping("/findOne/{id}")
    @Override
    public WmUser findWmUserById(@PathVariable("id") Long id) {
        return userService.getById(id);
    }
}
